package com.itheima.reggie.controller;

import com.itheima.reggie.config.R;
import com.itheima.reggie.dao.DishDao;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.DishDto;
import com.itheima.reggie.entity.Page;
import com.itheima.reggie.service.DishFlavor;
import com.itheima.reggie.service.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/dish")
public class DishController {
    @Autowired(required = false)
    private DishService dishService;
    /*
       保存菜品信息
        */
    @PostMapping
    public R saveDish(@RequestBody DishDto dishDto, HttpSession session){
        //1. 补全创建人与修改人
        Long empId = (Long) session.getAttribute("employee");
        dishDto.setCreateUser(empId);
        dishDto.setUpdateUser(empId);
        //2.把数据交给service
        dishService.save(dishDto);
        return R.success("添加成功");
    }
    /**
     * localhost:8080/dish/page?page=1&pageSize=10
     * get
     * 查询菜品信息
     */
    @GetMapping("/page")
    public R findList(@RequestParam(defaultValue = "1")Integer page,
                      @RequestParam(defaultValue ="10")Integer pageSize,
                      String name) {
     Page<DishDto>list=dishService.findAll(page,pageSize,name);
     return R.success(list);

    }
    /**
     * 修改
     */
    @GetMapping("/{id}")
    public R findDishdto(@PathVariable Long id){
       DishDto dishDto=dishService.findCatefory(id);
        return R.success(dishDto);
    }
    /**
     * 修改数据
     */
    @PutMapping
    public R updatDish(@RequestBody DishDto dishDto,HttpSession session){
        Long empId = (Long) session.getAttribute("employee");
        dishDto.setUpdateUser(empId);
       dishService.upDateDish(dishDto);
        return R.success("更新成功");
    }
    /**
     * http://localhost:8080/dish/list?categoryId=1397844303408574465
     * get
     */
    @GetMapping("/list")
    public R getDish(Long categoryId,Integer status){
        List<DishDto>dishList=dishService.findDishById(categoryId,status);
        return R.success(dishList);
    }
}
